package com.xing.ucenter.controller;


import com.xing.commonutils.JwtUtils;
import com.xing.commonutils.R;
import com.xing.commonutils.user.UcenterMemberOrder;
import com.xing.ucenter.controller.vo.LoginVo;
import com.xing.ucenter.controller.vo.RegisterVo;
import com.xing.ucenter.entity.LoginInfoVo;
import com.xing.ucenter.entity.Member;
import com.xing.ucenter.service.MemberService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author xingfuao
 * @since 2022-03-30
 */
@RestController
@RequestMapping("/educenter/member")
@ApiModel("根据用户id获取客户信息")
@CrossOrigin
public class MemberController {

    @Autowired
    private MemberService memberService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo) {
        String token = memberService.login(loginVo);
        return R.ok().data(token, token);
    }


    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("auth/getloginInfo")
    public R getloginInfo(HttpServletRequest request) {

        //工具类，解析token，获取用户的id值
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        LoginInfoVo loginInfoVo = memberService.getloginInfo(memberId);
        return R.ok().data("item", loginInfoVo);
    }

    //根据用户id获取用户信息
    //请求方式上啦
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){
        Member member = memberService.getById(id);
        //把member通过工具类浅拷贝给ucenterMemberOrder
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

}

